package test.gen;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.core.service.IService;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import org.stone.beecp.BeeDataSource;
import org.stone.beecp.BeeDataSourceConfig;

import java.nio.file.Paths;

/**
 * @author lingkang
 * created by 2025/7/19 23:17
 */
public class SystemCodegen {
    public static void main(String[] args) {
        //配置数据源
        BeeDataSourceConfig config = new BeeDataSourceConfig();
        config.setDriverClassName("com.mysql.jdbc.Driver");
        config.setJdbcUrl("jdbc:mysql://localhost:3306/fas");
        config.setUsername("root");
        config.setPassword("123456");
        BeeDataSource ds = new BeeDataSource(config);

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        //GlobalConfig globalConfig = createGlobalConfigUseStyle2();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(ds, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        // 设置输出目录
        globalConfig.setSourceDir(Paths.get(
                System.getProperty("user.dir"),
                "fast-admin",
                "src/main/java"
        ).toAbsolutePath().toString());
        globalConfig.getTemplateConfig()
                .setController(Paths.get(
                        System.getProperty("user.dir"),
                        "fast-generator",
                        "src/test/resources/enjoy/controller.tpl"
                ).toAbsolutePath().toString());
        globalConfig.getTemplateConfig()
                .setServiceImpl(Paths.get(
                        System.getProperty("user.dir"),
                        "fast-generator",
                        "src/test/resources/enjoy/serviceImpl.tpl"
                ).toAbsolutePath().toString());

        //设置根包
        globalConfig.setBasePackage("top.lingkang.fas.admin");

        //设置表前缀和只生成哪些表
        // globalConfig.setTablePrefix("sys_");
        globalConfig.setEntityClassSuffix("Entity");
        globalConfig.setMapperClassSuffix("Mapper");
        globalConfig.setServiceClassSuffix("Service");
        // globalConfig.setGenerateTable("sys_user");

        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);
        //设置项目的JDK版本，项目的JDK为14及以上时建议设置该项，小于14则可以不设置
        globalConfig.setEntityJdkVersion(17);

        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        globalConfig.setServiceImplGenerateEnable(true);
        globalConfig.setControllerGenerateEnable(true);

        //可以单独配置某个列
        /*ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("tenant_id");
        columnConfig.setLarge(true);
        columnConfig.setVersion(true);
        globalConfig.setColumnConfig("tb_account", columnConfig);*/

        // 配置service
        globalConfig.setServiceGenerateEnable(true);
        globalConfig.getServiceConfig()
                .setSuperClass(IService.class);
        globalConfig.getServiceImplConfig()
                .setSuperClass(ServiceImpl.class);



        return globalConfig;
    }
}
